#!/bin/bash
# Copyright (c) 2000-2016 Synology Inc. All rights reserved.

if [ -z "$__INCLUDE_ERRORS__" ]; then
__INCLUDE_ERRORS__=defined

#CheckErrorLog check_type
#CheckProjectStatus check_type project_name
CheckProjectStatus()
{
	local chk_type="$1"
	local logFile="$LogDir/$2.$1"
	local result=
	if [ ! -r "$logFile" ]; then
		return 1
	fi
	result=`sed '/tar\ zcpvf\ .../,/\[Done\]/d' "$logFile" | grep -E "line.*syntax error|Error" | grep -v "blib/arch/auto/Encode/Detect/Detector/Detector.so" | grep -v "Net-DNS-0.73" | grep -v 'checking for GPG Error - version' | grep -v 'this file was generated for autoconf 2.60' | grep -v "his may cause compile errors" | grep -v "In case of failure it is often undecidable if the error" | grep -v "I got the following error:" | grep -v "Error.pm" | grep -v "Error::Simple.3" | grep -v "Error.3" | grep -v "CPANPLUS::Error" | grep -v "Simple.pm" | grep -v ignored | grep -v Errors.py | grep -v CdsErrors  | grep -v distclean | grep -v PQresultErrorField | grep -v PQsetErrorVerbosity | grep -v hp2ps-Error | grep -v "Error Reporting support" | grep -v "Xapian::ErrorHandler" | grep -v SYNOErrorDump | grep -v GetErrorMsg | grep -v DelErrnoText | grep -v ErrorText | grep -v MVCPPrintError | grep -v ShowZError | grep -v ataPrintSmartErrorlog | grep -v SetMkdirError | grep -v SetRenameError | grep -v OutputError | grep -Ev 'Error\.(c|h|o|cpp|lo|pm|3|js|gif)' | grep -Ev '(glu|res|\.deps/|X[a-zA-Z]*)Error' | grep -v ErrorCode | grep -v TIFFSetErrorHandlerExt | grep -v SplashError | grep -v pkix_VerifyNode_FindError | grep -v PKIX_Error_Create | grep -v pkix_Error2ASCII | grep -v glib-Error-Reporting.html | grep -v savedError | grep -v ErrFHOSTGetMkfsError | grep -v "ImportError" | grep -v GenError | grep -v "tstLastError.py" | grep -v "libxml_xmlTextReaderGetErrorHandler" | grep -v "libxml_xmlParserCtxtGetErrorHandler" | grep -v "libxml_xmlErrorPtrWrap" | grep -v "ErrorFunc" | grep -v "\/Error" | grep -v "Error\/" |grep -v "Error::Simple" |grep -v "Writing Makefile for Error" | grep -v "SetValidErrors" | grep -Ev "skip client functions" | grep -v "struct cli_credentials" | grep -v "wxregex_regerror" | grep -v "wxThreadError" | grep -v "wxMutexError" | grep -v "wxCondError" | grep -v "wxSemaError" | grep -v "SYNOEAFixErrorAD" | grep -v "PyExc_AttributeError" | grep -v "MCErrorMessage" | grep -v gdSetErrorMethod | grep -v "inflated:" | grep -v "PolicyError" | grep -v "resolutionErrors" | grep -v "vmError_linux" | grep -v "error_messages.c" | grep -v "map2jdwpError" | grep -v "jvmtiError" | grep -v "socketTransport_getLastError" | grep -Ev '*.java' | grep -v "inflating:" | grep -v "OptionPaneError.wav" | grep -v "OutOfMemoryError" | grep -v "encoded value was" | grep -v "Encountered Infinity" | grep -v "libjvm.so" | grep -v "XErrorEvent" | grep -v "LEErrorCode" | grep -v "Error.gif" | grep -v "vmError" | grep -v "classFileError" | grep -v "Build project fail" | grep -v "eglGetError" | grep -v "exitErrorHandler" | grep -v "SLAP_CALLOC Error" | grep -v "Error scanning dquots" | grep -v "ErrorCategory"`

	if [ -z "$result" ]; then
		result=`grep -is "fatal error" $logFile` | `grep -v 'configure: Automatically print stack trace on fatal errors: no'`
	fi
	if [ -z "$result" ]; then
		result=`grep -is "*** missing separator (did you mean TAB instead of 8 spaces?)." $logFile`
	fi
	if [ -z "$result" ]; then
		result=`grep -is "No rule to make target" $logFile | grep -v distclean | grep -v clean`
	fi
	if [ -z "$result" ]; then
		result=`grep -is "don't know" $logFile | grep -v "make distclean" | grep -v "make clean"`
	fi
	if [ -z "$result" ]; then
		result=`grep -is "error:" $logFile | grep -v "make distclean" | grep -v "make clean" | grep -v "echo: write error: Broken pipe" | grep -v "error: Autoconf version 2.63 or higher is required" | grep -v "Error::Simple.3" | grep -v "ImportError" | grep -v "skip client functions" | grep -v "struct cli_credentials" | grep -v "Cannot chcon libjvm.so" | grep -v "I got the following error"`
	fi
	if [ "$chk_type" = "install" -a -z "$result" ]; then
		result=`grep -is "No such file or directory" $logFile | grep -v "ImportError" `
	fi

	if [ "$result" != "" ]; then
		echo -e "$result"
		return 2
	else
		return 0
	fi
}

fi
# vim:ft=sh
